/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sqlserver;

/**
 *
 * @author BEHIEN
 */
import java.util.ArrayList;
import java.util.List;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class CheckColumn {
    public static int CreateColumn(Column column) {
		if (column == null || column.getTableName() == ""
				|| column.getDBName() == "" || column.getColumnName() == ""
				|| column.getColumnType() == "")
			return MetaFile.ErrorInvalidAgrument;
		Document doc = MetaFile.LoadDoc();
		if (doc == null)
			return MetaFile.ErrorLoading;
		if (!CreateDatabase.CheckDatabaseExist(column.getDBName()))
			return MetaFile.ErrorDBNotFound;
		if (!Create.CheckTableExist(column.getDBName(), column.getTableName()))
			return MetaFile.ErrorTableNotFound;
		if (CheckColumnExist(column.getDBName(), column.getTableName(), column
				.getColumnName()))
			return MetaFile.ErrorExisted;
		try {
			XPath xpath = XPathFactory.newInstance().newXPath();
			XPathExpression expr = xpath.compile("//Database[@Name='"
					+ column.getDBName() + "']/Tables/Table[@Name='"
					+ column.getTableName() + "']/Columns");

			Element tbNode = (Element) expr.evaluate(doc, XPathConstants.NODE);
			Element newClNode = doc.createElement("Column");

			Attr attrName = doc.createAttribute("Name");
			attrName.setValue(column.getColumnName());
			newClNode.setAttributeNode(attrName);

			Attr attrType = doc.createAttribute("Type");
			attrType.setValue(column.getColumnType());
			newClNode.setAttributeNode(attrType);

			tbNode.appendChild(newClNode);
			if (MetaFile.Save(doc) == MetaFile.Success) {
				return MetaFile.Success;
			} else {
				return MetaFile.ErrorUnknown;
			}
		} catch (Exception e) {
			return MetaFile.ErrorUnknown;
		}
	}

	public static Column getColumnByName(String dbName, String tbName,
			String columnName) {
		try {
			Document doc = MetaFile.LoadDoc();
			XPath xpath = XPathFactory.newInstance().newXPath();
			XPathExpression expr = xpath.compile("//Database[@Name='" + dbName
					+ "']/Tables/Table[@Name='" + tbName
					+ "']/Columns/Column[@Name='" + columnName + "']");
			Element resultNode = (Element) expr.evaluate(doc,
					XPathConstants.NODE);
			if (resultNode != null) {
				String columnType = resultNode.getAttribute("Type");
				Column result = new Column(dbName, tbName, columnName,
						columnType);
				return result;
			}
		} catch (Exception e) {
			return null;
		}
		return null;
	}

	public static boolean CheckColumnExist(String dbName, String tbName,
			String columnName) {
		try {
			Document doc = MetaFile.LoadDoc();
			XPath xpath = XPathFactory.newInstance().newXPath();
			XPathExpression expr = xpath.compile("//Database[@Name='" + dbName
					+ "']/Tables/Table[@Name='" + tbName
					+ "']/Columns/Column[@Name='" + columnName + "']");
			Node result = (Node) expr.evaluate(doc, XPathConstants.NODE);
			if (result != null)
				return true;
		} catch (Exception e) {
			return false;
		}
		return false;
	}

	public static List<Column> getAllColumnInTable(String dbName, String tbName) {
		try {
			Document doc = MetaFile.LoadDoc();
			XPath xpath = XPathFactory.newInstance().newXPath();
			XPathExpression expr = xpath.compile("//Database[@Name='" + dbName
					+ "']/Tables/Table[@Name='" + tbName + "']/Columns/Column");
			NodeList listNodes = (NodeList) expr.evaluate(doc,
					XPathConstants.NODESET);
			if (listNodes != null) {
				List<Column> result = new ArrayList<Column>();
				for (int i = 0; i < listNodes.getLength(); i++) {
					Element itemNode = (Element) listNodes.item(i);
					Column item = new Column(dbName, tbName, itemNode
							.getAttribute("Name"), itemNode
							.getAttribute("Type"));
					result.add(item);
				}
				return result;
			}
		} catch (Exception e) {
			return null;
		}
		return null;

	}
}
